'use strict';

const { Controller } = require('egg');

class HomeController extends Controller {
  /**
   * h5 banner
   */
  async getH5Banner() {
    const { ctx } = this;
    const result = await ctx.service.home.getH5Banner();
    const res = {
      list: result.length > 0 ? result.map(it => ({
        id: it.banner_id,
        imageUrl: it.banner_url,
        desc: it.desc,
        sort: it.sort,
        status: it.status,
        createTime: it.created_at,
        updateTime: it.updated_at,
      })) : [],
    };
    ctx.helper.success(ctx, { data: res });
  }

  /**
   * 首页轮播图
   */
  async getAdminBanner() {
    const {
      ctx,
    } = this;
    const req = ctx.request.query;
    const result = await ctx.service.home.getAdminBanner(req);
    const res = {
      list: result.data.length > 0 ? result.data.map(it => ({
        bannerId: it.banner_id,
        imageUrl: it.banner_url,
        desc: it.desc,
        sort: it.sort,
        status: it.status,
        createTime: it.created_at,
        updateTime: it.updated_at,
      })) : [],
      pageData: result.pageData,
    };
    ctx.helper.success(ctx, { data: res });
  }

  /**
   * admin轮播图新增/编辑
   */
  async updateHomeBanner() {
    const {
      ctx,
    } = this;
    const data = ctx.request.body;
    try {
      ctx.validate({
        imageUrl: 'string',
        desc: 'string',
        sort: 'number',
      });
    } catch (error) {
      ctx.throw(400, '请求参数有误');
    }
    await ctx.service.home.updateHomeBanner(data);
    ctx.helper.success(ctx);
  }

  /**
   * h5首页
   */
  async h5home() {
    const { ctx } = this;
    const result = await ctx.service.home.h5home();
    ctx.helper.success(ctx);
  }

  /**
   * admin轮播图状态修改
   */
  async updateBannerStatus() {
    const {
      ctx,
    } = this;
    const data = ctx.request.body;
    try {
      ctx.validate({
        id: 'number',
        status: 'number',
      });
    } catch (error) {
      ctx.throw(400, '请求参数有误');
    }
    await ctx.service.home.updateHomeBanner(data);
    ctx.helper.success(ctx);
  }

  /**
   * 轮播图删除
   */
  async deleteBanner() {
    const { ctx } = this;
    const data = ctx.request.body;
    await ctx.service.home.deleteBanner(data);
    ctx.helper.success(ctx);
  }

  /**
   * admin首页推荐列表
   */
  async getRecommendList() {
    const { ctx } = this;
    await ctx.helper.sleep();
    const req = ctx.request.query;
    const result = await ctx.service.home.getRecommendList(req);
    const res = {
      list: result.data.length > 0 ? result.data.map(it => ({
        recommendId: it.recommend_id,
        imageUrl: it.image_url,
        description: it.description,
        price: it.price,
        sort: it.sort,
        status: it.status,
        createTime: it.createdAt,
        updateTime: it.updatedAt,
      })) : [],
      pageData: result.pageData,
    };
    ctx.helper.success(ctx, { data: res });
  }

  /**
   * 推荐新增、编辑
   */
  async updateRecommend() {
    const { ctx } = this;
    const data = ctx.request.body;
    await ctx.service.home.updateRecommend(data);
    ctx.helper.success(ctx);
  }

  /**
   * 推荐详情
   */
  async recommendDetail() {
    const { ctx } = this;
    const { recommendId } = ctx.request.query;
    const result = await ctx.service.home.recommendDetail(recommendId);
    const { recommend_id, image_url, description, detail, price, sort, status } = result;
    const res = {
      recommendId: recommend_id,
      imageUrl: image_url,
      description,
      detail,
      sort,
      price,
      status,
    };
    ctx.helper.success(ctx, { data: res });
  }

  /**
   * 推荐删除
   */
  async deleteRecommend() {
    const { ctx } = this;
    const data = ctx.request.body;
    await ctx.service.home.deleteRecommend(data);
    ctx.helper.success(ctx);
  }
}

module.exports = HomeController;
